import React from 'react';
import { Form, DatePicker, Select, Button, Table, Pagination } from 'antd';
import moment from 'moment';
import { connect } from 'dva';
import KnowledgeReadOnly from '@/pages/KnowledgeManagement/Overview/EditingKnowledge/knowledgeReadOnly';
import { postDocStatistics, exportDocList } from "@/services/datastatistics";

const { RangePicker } = DatePicker;

class KnolwedgeStatistics extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      list: [],
      total: 0,
      loading: true,
      current: 1,
      size: 10
    }
  }

  componentDidMount() {
    if (this.props.onRef) {
      this.props.onRef(this);
    }
  }

  /**
   * 获取表格数据
   */
  getList = () => {
    let { current, size } = this.state;
    const { dispatch, queryData } = this.props;
    this.setState({ loading: true }, async () => {
      let res = await postDocStatistics(queryData);
      console.log(res);
      this.setState({ list: res.records, total: res.total, loading: false })
    })
  }

  /**
   * 分页查询
   */
  queryByPageTprops = (page, size) => {
    this.props.queryByPage(page, size);
  }

  /**
   * 下载
   * @returns {*}
   */
  exportKnowledgeRecords = async () => {
    const { current, size } = this.state;
    const { dispatch, queryData } = this.props;
    queryData.current = current;
    queryData.size = size;
    const res = await exportDocList(queryData);
  }

  /**
   * 打开知识点查看页面
   * @returns {*}
   */
  openKnowledgeReadOnly = (row) => {
    this.KnowledgeReadOnly.showEditDrawer(row)
  }

  render() {
    const { list, total, loading } = this.state;
    const { queryData } = this.props;
    const columns = [
      {
        title: '文档',
        dataIndex: 'qaName',
        key: 'qaName',
        width: 400
      },
      {
        title: '分类',
        dataIndex: 'categoryName',
        key: 'categoryName',
        width: 300
      },
      {
        title: '命中次数',
        dataIndex: 'hitCount',
        key: 'hitCount',
        width: 200
      },
    ];

    return (
      <div>
        <Table
          dataSource={list}
          columns={columns}
          loading={loading}
          pagination={false}
          rowKey={(record, index) => index}
          scroll={{ x: "auto" }}
        />
        {total > 10 ? (
          <Pagination
            style={{ textAlign: 'right', marginTop: 16 }}
            total={total}
            current={queryData.current}
            onChange={this.queryByPageTprops}
            showQuickJumper={true}
            showSizeChanger={true}
            onShowSizeChange={this.queryByPageTprops}
          />
        ) : null}
        <KnowledgeReadOnly
          onRef={ref => this.KnowledgeReadOnly = ref}
          pageSource={'answerDetailStatis'}
        />
      </div>
    );
  }
}

export default connect(({ user }) => ({ // 全局数据
  currentUser: user.currentUser,
}))(Form.create()(KnolwedgeStatistics));
